home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MacStarter (THINK C 5.0⁄6.0) / globals-MacStarter.c < prev    next >
Text File  |  1994-01-25  |  15KB  |  592 lines

  1. /* MacStarter is a shell for simple Macintosh applications shell written in
  2.    THINK C, with some small use of its object-oriented features.
  3.    The whole system is described in a README file tha should be in 
  4.    the same folder with this file.  There are no comments on this file.
  5.    You should not need to know the implementation of any of the functions
  6.    written here.  Information on what is available from this file for
  7.    you to use elsewhere in your program is in the README file.
  8. */
  9.  
  10. #include "globals-MacStarter.h"
  11. #include "stdlib.h"
  12.  
  13. EventRecord gEvent;
  14. int gClickCount = 0;
  15. long lastClickTime = 0;
  16. Point lastClickPt = { 0,0 };
  17. Rect gWindowRect = { 0,0,0,0 };
  18. xWindow *xWindowList = 0;
  19.  
  20. int xWindow::Window2XWindow(WindowPtr win, xWindow** xWin) {
  21.    xWindow *run;
  22.    run = xWindowList;
  23.    while ( run && run->theWindow != win )
  24.       run = run->nextWindow;
  25.    *xWin = run;
  26.    return (run != 0);
  27. }
  28.  
  29.  
  30. void xWindow::SetDefaults(void) {
  31.   features = hasHScroll + hasVScroll + hasGoAway + hasZoom + hasGrow;
  32.   topScrollOffset = 0;
  33.   bottomScrollOffset = 0;
  34.   leftScrollOffset = 0;
  35.   rightScrollOffset = 0;
  36.   minH = 50;
  37.   minV = 50;
  38.   maxH = 32000;
  39.   maxV = 32000;
  40.   hLinesPerPage = 1;
  41.   vLinesPerPage = 1;
  42. }
  43.  
  44. void xWindow::OpenInRect(Str255 title, int left, int top, int right, int bottom) {
  45.    int height,width;
  46.    height = screenBits.bounds.bottom - screenBits.bounds.top - 20;
  47.    width = screenBits.bounds.right - screenBits.bounds.left;
  48.    if ( right - left <= 0 || right - left > width) {
  49.       left = 2;
  50.       right = screenBits.bounds.right - 2;
  51.    };
  52.    if ( bottom - top <= 0 || bottom - top > height) {
  53.       top = 40;
  54.       bottom = screenBits.bounds.bottom - 2;
  55.    };
  56.    SetDefaults();
  57.    doBasicOpen(title,left,top,right,bottom);
  58. }
  59.  
  60. void xWindow::OpenFullScreen(Str255 title) {
  61.    Rect R;
  62.    R = screenBits.bounds;
  63.    R.top = 36;
  64.    InsetRect(&R,4,4);
  65.    OpenInRect(title,R.left,R.top,R.right,R.bottom);
  66. }
  67.  
  68. void xWindow::Open(Str255 title) {
  69.    if (gWindowRect.bottom <= gWindowRect.top) {
  70.       gWindowRect = screenBits.bounds;
  71.       gWindowRect.top += 42;
  72.       gWindowRect.left += 4;
  73.       gWindowRect.right = gWindowRect.left + 3*(gWindowRect.right-gWindowRect.left)/4;
  74.       gWindowRect.bottom = gWindowRect.top + 3*(gWindowRect.bottom-gWindowRect.top)/4;   
  75.    };
  76.    if (gWindowRect.bottom > screenBits.bounds.bottom)
  77.       OffsetRect(&gWindowRect,0,40 - gWindowRect.top);
  78.    if (gWindowRect.right > screenBits.bounds.right)
  79.       OffsetRect(&gWindowRect,4 - gWindowRect.left, 0);
  80.    OpenInRect(title,gWindowRect.left,gWindowRect.top,gWindowRect.right,gWindowRect.bottom);
  81.    OffsetRect(&gWindowRect,20,20);   
  82. }
  83.  
  84.  
  85. void xWindow::doBasicOpen(Str255 title, int left, int top, int right, int bottom) {
  86.    Rect bounds;
  87.    short procID;
  88.    WindowPtr win;
  89.    nextWindow = xWindowList;
  90.    xWindowList = this;
  91.    SetRect(&bounds,left,top,right,bottom);
  92.    if (features & hasGrow)
  93.       procID = documentProc;
  94.    else
  95.       procID = noGrowDocProc;
  96.    if (features & hasZoom)
  97.       procID += 8;
  98.    theWindow = NewWindow( (Ptr) 0, &bounds, title, true, procID, (WindowPtr) -1, features & hasGoAway, 0);
  99.    win = theWindow;
  100.    if (features & hasHScroll) {
  101.       SetRect(&bounds,0,0,1,1);
  102.       hScroll = NewControl(win,&bounds,"\p",0,0,0,0,scrollBarProc,(long) this);
  103.    };
  104.    if (features & hasVScroll) {
  105.       SetRect(&bounds,0,0,1,1);
  106.       vScroll = NewControl(win,&bounds,"\p",0,0,0,0,scrollBarProc,(long) this); 
  107.    };
  108.    adjustToNewSize();
  109. }
  110.  
  111.  
  112. xWindow::~xWindow(void) {
  113.  
  114. }
  115.  
  116. short xWindow::Close(void) {
  117.    xWindow *run;
  118.    run = xWindowList;
  119.    if (run == this) {
  120.       xWindowList = xWindowList->nextWindow;
  121.    } else {
  122.       while ( run && run->nextWindow != this ) {
  123.           run = run->nextWindow;  
  124.       };
  125.       if (run) {
  126.           run->nextWindow = run->nextWindow->nextWindow;
  127.       };
  128.    };
  129.    DisposeWindow(theWindow);
  130.    delete this;
  131.    return 1;
  132. }
  133.  
  134.  
  135.  
  136. void xWindow::doContentClick(Point localPt) {
  137.     /* Default action is to do nothing */
  138. }
  139.  
  140. void xWindow::doKey(char ch) {
  141.     /* Default action is to do nothing */
  142. }
  143.  
  144. void xWindow::doHScroll(int dh) {
  145.    /* Default action is to erase and redraw */
  146.    GrafPtr savePort;
  147.    Rect badRect;
  148.    GetPort(&savePort);
  149.    SetPort(theWindow);
  150.    badRect = theWindow->portRect;
  151.    if ( features & hasHScroll )
  152.       badRect.bottom -= 15;
  153.    if ( features & hasVScroll )
  154.       badRect.right -= 15;
  155.    EraseRect(&badRect);
  156.    doRedraw(&badRect);
  157.    SetPort(savePort);
  158. }
  159.  
  160. void xWindow::doVScroll(int dv) {
  161.    /* Default action is to erase and redraw */
  162.    GrafPtr savePort;
  163.    Rect badRect;
  164.    GetPort(&savePort);
  165.    SetPort(theWindow);
  166.    badRect = theWindow->portRect;
  167.    if ( features & hasHScroll )
  168.       badRect.bottom -= 15;
  169.    if ( features & hasVScroll )
  170.       badRect.right -= 15;
  171.    EraseRect(&badRect);
  172.    doRedraw(&badRect);
  173.    SetPort(savePort);
  174. }
  175.  
  176. void xWindow::doRedraw(Rect* badRect) {
  177.    /* default method does nothing */
  178. }
  179.  
  180.  
  181. void xWindow::doEvent(void) {
  182.    GrafPtr savePort;
  183.    switch (gEvent.what) {
  184.      case autoKey:
  185.      case keyDown: {
  186.        GetPort(&savePort);
  187.        SetPort(theWindow);
  188.        doKey(gEvent.message & 0xFF);
  189.        SetPort(savePort);
  190.        break; 
  191.      }
  192.      case mouseDown: {
  193.        doClick(gEvent.where);
  194.        break;
  195.      }
  196.      case activateEvt: {
  197.        doActivate(gEvent.modifiers & activeFlag);
  198.        break;
  199.      }
  200.      case updateEvt: {
  201.        doUpdate();
  202.      }
  203.    }
  204. }
  205.  
  206. /* Global Data for communication of doClick with ContinuousScroll */
  207.  
  208. static xWindow *scrollingWin;
  209.  
  210. pascal void xWindow::ContinuousScroll(ControlHandle theControl, short partNum) {
  211.    short change;
  212.    short oldVal, max;
  213.    short scrollLinesPerPage;
  214.    short scrollingHorizontally;
  215.    if ( theControl == scrollingWin->hScroll ) {
  216.       scrollingHorizontally = 1;
  217.       scrollLinesPerPage = scrollingWin->hLinesPerPage;
  218.    }
  219.    else {
  220.       scrollingHorizontally = 0;
  221.       scrollLinesPerPage = scrollingWin->vLinesPerPage;
  222.    };
  223.    oldVal = GetCtlValue(theControl);
  224.    max = GetCtlMax(theControl);
  225.    change = 0;
  226.    if (partNum == inDownButton) {
  227.       if (oldVal < max)
  228.          change = 1;
  229.    }
  230.    else if (partNum == inUpButton) {
  231.       if (oldVal > 0)
  232.          change = -1;
  233.    }
  234.    else if (partNum == inPageDown) {
  235.       if (oldVal + scrollLinesPerPage  <= max)
  236.          change = scrollLinesPerPage;
  237.       else if (oldVal < max)
  238.          change = max - oldVal;
  239.    }
  240.    else if (partNum == inPageUp) {
  241.       if (oldVal - scrollLinesPerPage >0)
  242.          change = -scrollLinesPerPage;
  243.       else if (oldVal > 0) 
  244.          change = -oldVal;
  245.    };
  246.    if (change) {
  247.       SetCtlValue(theControl,oldVal + change);
  248.       if (scrollingHorizontally)
  249.          scrollingWin->doHScroll(change);
  250.       else
  251.          scrollingWin->doVScroll(change);
  252.    }
  253. }
  254.  
  255. void xWindow::doClick(Point globalPt) {
  256.    short partNum;
  257.    GrafPtr savePort;
  258.    WindowPtr win;
  259.    Point localPt;
  260.    short part;
  261.    short oldVal;
  262.    ControlHandle theControl;
  263.    partNum = FindWindow(globalPt,&win);
  264.    if (theWindow != FrontWindow() && partNum != inDrag) {
  265.      SelectWindow(theWindow);
  266.      lastClickTime = 0;
  267.      return;
  268.    };
  269.    if (TickCount() - lastClickTime <= GetDblTime() 
  270.           && abs(globalPt.h - lastClickPt.h) <= 3
  271.           && abs(globalPt.v - lastClickPt.v) <= 3 )
  272.       gClickCount++;
  273.    else
  274.       gClickCount = 0;
  275.    lastClickPt = globalPt;
  276.    lastClickTime = TickCount();
  277.    switch (partNum) {
  278.      case inGrow: {
  279.        doGrow(globalPt);
  280.        break;
  281.      }
  282.      case inGoAway: {
  283.        if (TrackGoAway(theWindow,globalPt))
  284.           Close();
  285.        break;
  286.      }
  287.      case inDrag: {
  288.        doDrag(globalPt);
  289.        break;
  290.      }
  291.      case inZoomIn: {
  292.        doZoom(partNum);
  293.        break;
  294.      }
  295.      case inZoomOut: {
  296.        doZoom(partNum);
  297.        break;
  298.      }
  299.      case inContent: {
  300.         GetPort(&savePort);
  301.         SetPort(theWindow);
  302.         localPt = globalPt;
  303.         GlobalToLocal(&localPt);
  304.         if ( (localPt.v > theWindow->portRect.bottom - 15 && (features & hasHScroll))
  305.             || (localPt.h > theWindow->portRect.right - 15 && (features & hasVScroll)) ) {
  306.           if ( (part = FindControl(localPt,theWindow,&theControl))
  307.                && ( theControl == hScroll || theControl == vScroll ) ) {
  308.              if (part == inThumb) {
  309.                 oldVal = GetCtlValue(theControl);
  310.                 if (TrackControl(theControl,localPt,0L)) {
  311.                    if (theControl == hScroll)
  312.                       doHScroll(GetCtlValue(theControl) - oldVal);
  313.                    else
  314.                       doVScroll(GetCtlValue(theControl) - oldVal);
  315.                 }
  316.              }
  317.              else {
  318.                 scrollingWin = this;
  319.                 TrackControl(theControl,localPt,&xWindow::ContinuousScroll);
  320.              }
  321.           }
  322.         }
  323.         else {
  324.           doContentClick(localPt);
  325.         };
  326.         SetPort(savePort);
  327.      }
  328.    } 
  329. }
  330.  
  331.  
  332. void xWindow::doDrag(Point globalPt) {
  333.    Rect dragRect;
  334.    WindowPtr win;
  335.    dragRect = screenBits.bounds;
  336.    InsetRect(&dragRect,4,4);
  337.    dragRect.top -= 15;
  338.    win = theWindow;
  339.    DragWindow(win,globalPt,&dragRect);
  340. }
  341.  
  342.  
  343. void xWindow::doZoom(short partNum) {
  344.    Rect R;
  345.    WindowPtr win;
  346.    GrafPtr savePort;
  347.    R = theWindow->portRect;
  348.    win = theWindow;
  349.    GetPort(&savePort);
  350.    SetPort(win);
  351.    EraseRect(&R);
  352.    ZoomWindow(win,partNum,0);
  353.    InvalRect(&(win->portRect));
  354.    if ( features & hasHScroll )
  355.       HideControl(hScroll);
  356.    if ( features & hasVScroll )
  357.       HideControl(vScroll);
  358.    SetPort(savePort);
  359.    adjustToNewSize();
  360. }
  361.  
  362.  
  363. void xWindow::doGrow(Point globalPt) {
  364.    Rect sizeRect;
  365.    WindowPtr win;
  366.    GrafPtr savePort;
  367.    short height,width;
  368.    long newSize;
  369.    if ( minH < 1 || maxH <= minH ) {
  370.       minH = 50;
  371.       maxH = 32000;
  372.    };
  373.    if ( minV < 1 || maxV <= minV ) {
  374.       minV = 50;
  375.       maxV = 32000;
  376.    };
  377.    SetRect(&sizeRect,minH,minV,maxH,maxV);
  378.    win = theWindow;
  379.    if ( newSize = GrowWindow(theWindow,globalPt,&sizeRect) ) {
  380.       height = HiWord(newSize);
  381.       width = LoWord(newSize);
  382.       GetPort(&savePort);
  383.       SetPort(win);
  384.       EraseRect(&(win->portRect));
  385.       SizeWindow(win,width,height,0);
  386.       InvalRect(&(win->portRect));
  387.       if ( features & hasHScroll )
  388.          HideControl(hScroll);
  389.       if ( features & hasVScroll )
  390.          HideControl(vScroll);
  391.       SetPort(savePort);
  392.       adjustToNewSize();
  393.    }
  394. }
  395.  
  396.  
  397. void xWindow::doUpdate(void) {
  398.    GrafPtr savePort;
  399.    WindowPtr win;
  400.    Rect badRect;
  401.    Point savePen;
  402.    long saveColor;
  403.    win = theWindow;
  404.    GetPort(&savePort);
  405.    SetPort(win);
  406.    BeginUpdate(win);
  407.    EraseRect(&(win->portRect));
  408.    DrawControls(win);
  409.    if ( features & hasGrow ) {
  410.       DrawGrowIcon(win);
  411.       savePen = theWindow->pnSize;
  412.       saveColor = theWindow->fgColor;
  413.       PenSize(1,1);
  414.       ForeColor(whiteColor);
  415.       if ( !(features & hasVScroll) ) {
  416.          MoveTo(win->portRect.right - 15, 0);
  417.          LineTo(win->portRect.right - 15, win->portRect.bottom - 16);
  418.       }
  419.       else if (topScrollOffset > 1) {
  420.          MoveTo(win->portRect.right - 15, 0);
  421.          LineTo(win->portRect.right - 15, topScrollOffset - 2);
  422.       }
  423.       if ( !(features & hasHScroll) ) {
  424.          MoveTo(0,win->portRect.bottom - 15);
  425.          LineTo(win->portRect.right - 16, win->portRect.bottom - 15);
  426.       }
  427.       else if ( leftScrollOffset > 1 ) {
  428.          MoveTo(0,win->portRect.bottom - 15);
  429.          LineTo(leftScrollOffset - 2, win->portRect.bottom - 15);
  430.       };
  431.       PenSize(savePen.h,savePen.v);
  432.       ForeColor(saveColor);
  433.    };
  434.    badRect = (*(win->visRgn))->rgnBBox;
  435.    doRedraw(&badRect);
  436.    EndUpdate(win);
  437.    SetPort(savePort);
  438. }
  439.  
  440.  
  441. void xWindow::doActivate(int active) {
  442.    GrafPtr savePort;
  443.    Rect R;
  444.    if (active) {
  445.      if ( features & hasVScroll )
  446.         ShowControl(vScroll);
  447.      if ( features & hasHScroll )
  448.         ShowControl(hScroll);
  449.    }
  450.    else {
  451.      if ( features & hasVScroll )
  452.         HideControl(vScroll);
  453.      if ( features & hasHScroll )
  454.         HideControl(hScroll);
  455.    };
  456.    if (hasGrow & features) {
  457.       R = theWindow->portRect;
  458.       R.left = R.right - 14;
  459.       R.top = R.bottom - 14;
  460.       GetPort(&savePort);
  461.       SetPort(theWindow);
  462.       if (active)
  463.          InvalRect(&R);
  464.       else
  465.          EraseRect(&R);
  466.       SetPort(savePort);
  467.    };
  468. }
  469.  
  470.  
  471. void xWindow::adjustToNewSize(void) {
  472.    Rect bounds;
  473.    short height,width;
  474.    WindowPtr win;
  475.    win = theWindow;
  476.    if (features & hasHScroll) {
  477.       bounds = win->portRect;
  478.       bounds.top = bounds.bottom - 15;
  479.       bounds.bottom++;
  480.       bounds.left--;
  481.       if (features & hasGrow)
  482.          bounds.right -= 14;
  483.       else
  484.          bounds.right++;
  485.       bounds.left += leftScrollOffset;
  486.       bounds.right -= rightScrollOffset;
  487.       if (bounds.right <= bounds.left)
  488.          bounds.right = bounds.left + 5;
  489.       height = bounds.bottom-bounds.top;
  490.       width = bounds.right-bounds.left;
  491.       MoveControl(hScroll,bounds.left,bounds.top);
  492.       SizeControl(hScroll,width,height);
  493.       ShowControl(hScroll);
  494.    };
  495.    if (features & hasVScroll) {
  496.       bounds = win->portRect;
  497.       bounds.left = bounds.right - 15;
  498.       bounds.right++;
  499.       bounds.top--;
  500.       if ((features & hasGrow) || (features & hasHScroll))
  501.          bounds.bottom -= 14;
  502.       else
  503.          bounds.bottom++;
  504.       bounds.top += topScrollOffset;
  505.       bounds.bottom -= bottomScrollOffset;
  506.       if (bounds.bottom <= bounds.top)
  507.          bounds.bottom = bounds.top + 5;
  508.       height = bounds.bottom-bounds.top;
  509.       width = bounds.right-bounds.left;
  510.       MoveControl(vScroll,bounds.left,bounds.top);
  511.       SizeControl(vScroll,width,height);
  512.       ShowControl(vScroll);
  513.    };
  514. }
  515.  
  516. void xWindow::ForceRedraw(Rect *badRect) {
  517.    GrafPtr savePort;
  518.    GetPort(&savePort);
  519.    SetPort(theWindow);
  520.    if (!badRect || EmptyRect(badRect))
  521.       InvalRect(&theWindow->portRect);
  522.    else
  523.       InvalRect(badRect);
  524.    SetPort(savePort);
  525. }
  526.  
  527. void xWindow::GetTitle(Str255 name) {
  528.    GetWTitle(theWindow,name);
  529. }
  530.  
  531. void xWindow::SetTitle(Str255 name) {
  532.    SetWTitle(theWindow,name);
  533. }
  534.  
  535. int xWindow::GetHVal(void) {
  536.    return (features & hasHScroll)? GetCtlValue(hScroll) : 0;
  537. }
  538.  
  539. int xWindow::GetVVal(void) {
  540.    return (features & hasVScroll)? GetCtlValue(vScroll) : 0;
  541. }
  542.  
  543. void xWindow::SetHVal(int val) {
  544.    short oldVal;
  545.    short dh;
  546.    if (features & hasHScroll) {
  547.       oldVal = GetCtlValue(hScroll);
  548.       SetCtlValue(hScroll,val);
  549.       dh = GetCtlValue(hScroll) - oldVal;
  550.       if (dh) doHScroll(dh);
  551.    }
  552. }
  553.  
  554. void xWindow::SetVVal(int val) {
  555.    short oldVal;
  556.    short dv;
  557.    if (features & hasVScroll) {
  558.       oldVal = GetCtlValue(vScroll);
  559.       SetCtlValue(vScroll,val);
  560.       dv = GetCtlValue(vScroll) - oldVal;
  561.       if (dv) doHScroll(dv);
  562.    }
  563. }
  564.  
  565. void xWindow::SetHLinesPerPage(short hLines) {
  566.    if (hLines > 0)
  567.       hLinesPerPage = hLines;
  568. }
  569.  
  570. void xWindow::SetVLinesPerPage(short vLines) {
  571.    if (vLines > 0)
  572.       vLinesPerPage = vLines;
  573. }
  574.  
  575. int xWindow::GetVMax(void) {
  576.    return (hasVScroll & features)? GetCtlMax(vScroll) : 0;
  577. }
  578.  
  579. int xWindow::GetHMax(void) {
  580.    return (hasHScroll & features)? GetCtlMax(hScroll) : 0;
  581. }
  582.  
  583. void xWindow::SetHMax(int max) {
  584.    if (max >= 0 && (features & hasHScroll))
  585.       SetCtlMax(hScroll,max);
  586. }
  587.  
  588. void xWindow::SetVMax(int max) {
  589.    if (max >= 0 && (features & hasVScroll))
  590.       SetCtlMax(vScroll,max);
  591. }
  592.